package lin.an.user.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lin.an.exception.CommonException;
import lin.an.food.entity.Food;
import lin.an.food.param.FoodPageParam;
import lin.an.pojo.CommonResult;
import lin.an.user.Param.*;
import lin.an.user.entity.User;
import lin.an.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller层
 * @author zhoulinq
 * @date 2025/3/10
 */
@RestController
@RequestMapping("/delicious/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 获取用户信息
     */
    @GetMapping("/detail")
    public CommonResult<User> detailOne(){
        return CommonResult.data(userService.detailOne());
    }
    /**
     * 增加用户
     */
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody UserAddParam userAddParam){
        userService.add(userAddParam);
        return CommonResult.ok("添加成功");
    }

    /**
     * 删除用户(批量方法)
     */
    @PostMapping("/detele")
    public CommonResult<String> detele(@RequestBody List<UserDeleteParam> userDeleteParamList){
        userService.detele(userDeleteParamList);
        return CommonResult.ok();
    }

    /**
     * 修改用户
     */
    @PostMapping("/updata")
    public CommonResult<String> edit(@RequestBody UserEditParam userEditParam){
        userService.edit(userEditParam);
        return CommonResult.ok();
    }

    /**
     * 查询用户(分页查询)
     */
    @PostMapping("/page")
    public CommonResult<Page<User>> page(@RequestBody UserPageParam userPageParam){
        return CommonResult.data(userService.page(userPageParam));
    }
    /**
     * 模糊查询用户，根据用户名查询
     */
    @PostMapping("/like")
    public CommonResult<List<User>> like(@RequestBody UserLikeParam userLikeParam){
        return CommonResult.data(userService.like(userLikeParam));
    }
    /**
     * 获取收藏列表
     */
    @GetMapping("/favorite/list")
    public CommonResult<List<Food>> favoriteList() {
        List<Food> favoriteList = userService.favoriteList();
        if (favoriteList.isEmpty()) {
            return CommonResult.error("暂无收藏");
        }
        return CommonResult.data(favoriteList);
    }
    /**
     * 用户找回密码
     */
    @PostMapping("/reset/password")
    public CommonResult<String> resetPassWord(@RequestBody UserResetParam userResetParam){

        return CommonResult.data(userService.resetPassWord(userResetParam));
    }
    /**
     * 获得总用户数
     */
    @GetMapping("/amount")
    public CommonResult<Long> userAmount(){
        return CommonResult.data(userService.amount());
    }

    /**
     * 封禁用户
     */
    @PostMapping("/off")
    public CommonResult<String> banUser(@RequestBody UserBanParam userBanParam) {
        userService.banUser(userBanParam.getUserId());
        return CommonResult.ok("用户已被封禁");
    }

    /**
     * 解封用户
     */
    @PostMapping("/open")
    public CommonResult<String> unbanUser(@RequestBody UserBanParam userBanParam) {
        userService.unbanUser(userBanParam.getUserId());
        return CommonResult.ok("用户已被解封");
    }

}
